package CtrLayer;
import java.util.ArrayList;

import DBLayer.ProductDB;
import DBLayer.ProductDBIF;
import ModelLayer.DraughtBeer;
import ModelLayer.Drink;
import ModelLayer.Product;


/**
 *ProductCtr
 *This class handles methods related to products.
 *31/05/2012
 *@author Bager
 *@version 0.1
 */

public class ProductCtr {

	private static ProductCtr instance;

	/**
	 * Creates a new productCtr.
	 */

	private ProductCtr()
	{

	}

	/**
	 * Returns a instance of ProductCtr if a ProductCtr already exist, creates a new ProductCtr and returns it if a ProductCtr don't already exist.
	 * @return A instance of ProductCtr. 
	 */

	public static ProductCtr getInstance()
	{
		if(instance==null)
		{
			instance = new ProductCtr();
		}

		return instance;
	}

	/**
	 * Finds the product with a matching productId.
	 * @param productId The productId of the product to return.
	 * @return The product with a matching productId.
	 */

	public Product findProduct(int productId)
	{
		ProductDBIF dbProduct = new ProductDB();
		return dbProduct.findProduct(productId, false);
	}

	public DraughtBeer findDraughtBeer(int productId)
	{
		ProductDBIF dbProduct = new ProductDB();
		return dbProduct.findDraughtBeer(productId, false);
	}

	public void downsizeInventory(Product product, int amount)
	{
		ProductDBIF dbProduct = new ProductDB();

		if(product.getProductType().equals("Draught"))
		{
			
			dbProduct.downSizeInventory(product.getProductId(), amount*50);
		}

		else
		{
			if(product.getProductType().equals("Drink"))

			{
				ArrayList<Product> products = ((Drink)product).getProducts();
				for(Product product2 : products)
				{
					dbProduct.downSizeInventory(product2.getProductId(), amount*2);
				}
			}


			else
			{
				if(product.getProductType() != "Drink" || product.getProductType() != "Draught")
				{
					dbProduct.downSizeInventory(product.getProductId(), amount);
				}

			}
		}
	}

}
